const ajax = require('../../../utils/ajax/ajax.js')
const util = require('../../../utils/util.js')
Page({
    data: {
        nickName: '',
        avatar: '',
        rec_points: 0, // 推荐积分
        sob_points: 0, // 收益积分
        grade: 0
    },
    props: {
        rec_code: '',
        freeze_points: ''
    },
    onLoad: function () {
        this.getMyInfo()
    },
    getUserProfile() {
        const that = this
        wx.getUserProfile({
            desc: '展示用户信息',
            success(res) {
                const nickName = res.userInfo.nickName
                const avatar = res.userInfo.avatarUrl
                that.setData({
                    nickName: nickName,
                    avatar: avatar
                })
                ajax.post({
                    url: '/wx/submitUserInfo',
                    data: {
                        nick_name: nickName,
                        avatar: avatar
                    }
                })
            },
            fail() {
                util.toast('请完善您的个人信息')
            }
        })
    },
    getMyInfo() {
        const that = this
        ajax.post({
            url: '/wx/getMyInfo',
            success(res) {
                const data = res.data
                const { nick_name,avatar } = data.wxInfo
                const { total_rec_points,total_sob_points,rec_code,grade,freeze_points } = data.user
                that.setData({
                    nickName: nick_name,
                    avatar: avatar,
                    rec_points: total_rec_points,
                    sob_points: total_sob_points,
                    grade: grade 
                })
                that.props.rec_code = rec_code
                that.props.freeze_points = freeze_points
            }
        })
    },
    // 推荐积分 弹框
    showPopupRe() {
        const popup = this.selectComponent('#popupRec');
        popup.showPopup();
    },
    // 推荐积分 弹框
    showPopupSob() {
        const popup = this.selectComponent('#popupSob');
        popup.showPopup();
    },
    // 跳转我的认购 buyList
    toBuyList() {
        wx.navigateTo({
            url: '/pages/Mine/buyList/buyList',
        })
    },
    toMyTeam() {
        const { avatar, nickName, grade } = this.data
        wx.navigateTo({
            url: `/pages/Mine/myTeam/myTeam?avatar=${avatar}&nickName=${nickName}&grade=${grade}`,
        })
    },
    toRecCode() {
        const recCode = this.props.rec_code
        wx.navigateTo({
            url: '/pages/Mine/recCode/recCode?recCode=' + recCode,
        })
    },
    toPointRecord() {
        const { rec_points, sob_points } = this.data
        const freeze_points = this.props.freeze_points
        wx.navigateTo({
            url: `/pages/Mine/pointRecord/pointRecord?rec_points=${rec_points}&sob_points=${sob_points}&freeze_points=${freeze_points}`,
        })
    },
    toMyRecom() {
        wx.navigateTo({
            url: '/pages/Mine/myRecom/myRecom',
        })
    },
    showCallPopup() {
        const popupRefund = this.selectComponent('#popupCall');
        popupRefund.showPopup();
        
    },
    confirmCall() {
        this.showCallPopup()
        wx.makePhoneCall({
            phoneNumber: '13757189142'
        })
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.getMyInfo()
        wx.stopPullDownRefresh()
    }
})